<?php
/**
 * EgpusersPagingBean.class.php
 * 
 * (C) 2009, tetsuya.odaka(EzoGP).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* 
 * ページングする画面を生成するためのオブジェクトです。
 * 
 * author；	tetsuya.odaka　
 * date;	2009/6/3
 * 
 * Myznala/AbstructPagingBeanを継承しています。
 *
*/

require_once('../Myznala/AbstructPagingBean.class.php');

class EgpusersPagingBean extends AbstructPagingBean{

	// コンストラクタ
	function __construct(){
		parent::__construct(); /* 親クラスのコンストラクタを呼び出す */

		// 親のプロパティーへ代入
		$this->_TableName="EGP_USERS";
	}
	
	// overload
	function setTotalRecordsNum($conn, $id, $name){
		require("../Myznala/debugLog.php");
		$sql = 'select count(*) from '.$this->_TableName.' where id like "'.$id.'%" and name like "'.$name.'%";';
//		$myDebugLog->debug("sql: ".$sql);
		$result = $conn->query($sql);
		if(MDB2::isError($result)){
			die($result->getMessage());
		}
		
		$row = $result->fetchRow(MDB2_FETCHMODE_ORDERED);
		$this->_TotalRecordsNum=$row[0];
//		$myDebugLog->debug("row[0]: ".$row[0]);
		return;
	}
	
	function calc($conn, $id, $name){
		$this->setTotalRecordsNum($conn, $id, $name);
		$this->setTotalPageNum();
		return;
	}

	/* 指定されたページへ遷移する */
	function goPage($conn, $goto, $id, $name){
		require("../Myznala/debugLog.php");
		require_once('EgpusersDao.class.php');

//		$id="'".$id."%'";
//		$name="'".$name."%'";

		$sql = 'select * from EGP_USERS where id like "'.$id.'%" and name like "'.$name.'%";';
//		$myDebugLog->debug("sql: ".$sql);
		$result = $conn->query($sql);
		if(MDB2::isError($result)){
			die($result->getMessage());
		}
		
		$i=0;
		$result_array=array();
		$start = ($goto - 1) * $this->_PageSize;
		$end = min($start+($this->_PageSize)-1, $this->_TotalRecordsNum-1);
		
//		$myDebugLog->debug("$this->_TotalRecordsNum: ".$this->_TotalRecordsNum);
//		$myDebugLog->debug("$this->_PageSize: ".$this->_PageSize);
		foreach (range($start, $end) as $rowNum) {
			if (!$row = &$result->fetchRow(MDB2_FETCHMODE_ASSOC, $rowNum)) {
				break;
			} 
			$obj=new EgpusersDao();
			$obj->setId($row["id"]);
			$obj->setEmail($row["email"]);
			$obj->setName($row["name"]);
			$obj->setNameYomi($row["name_yomi"]);
			$obj->setRole($row["role"]);
			$result_array[$i]=$obj;
			$i++;
		}
		$result->free();

		return $result_array;
	}
		
	/* 最初のページへ遷移する */
	// overload
	function goTop($conn, $id, $name){
		return goPage($conn,1,$id, $name);
	}

	/* 最後のページへ遷移する */
	// overload
	function goBottom($conn, $id, $name){
		return goPage($conn, $this->_TotalPageNum, $id, $name);
	}
}
?>